package linkedList.双指针.剑指;

import linkedList.BaseLinkedListTest;
import po.ListNode;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 剑指offer链表中倒数第k个节点22.java
 * @Description TODO https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/
 * @createTime 2021年03月21日 11:36:00
 */
public class 剑指offer链表中倒数第k个节点22 extends BaseLinkedListTest {
    public static ListNode getKthFromEnd(ListNode head, int k) {
        if (head == null) {
            return head;
        }
        ListNode firstPoint = head, secondPoint = head;
        for (int i = 0; i < k; i++) {
            secondPoint = secondPoint.next;
        }
        while (secondPoint!=null){
            firstPoint=firstPoint.next;
            secondPoint=secondPoint.next;
        }
        return firstPoint;
    }

    public static void main(String[] args) {
        getKthFromEnd(createLinkedList(1,2,3,4,5),2);
    }
}
